package com.techstar.api.service.hinge_situation;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.techstar.api.pojo.hinge_situation.HingeSituationPassengerRankVolume;
import com.techstar.api.pojo.hinge_situation.HingeSituationPassengerVolume;
import com.techstar.api.repos.hinge_situation.HingeSituationPassengerRankVolumeRepos;
import com.techstar.api.repos.hinge_situation.HingeSituationPassengerVolumeRepos;
import com.techstar.api.service.ConfigProperties;
import com.techstar.api.util.GateWayApiProperties;
import com.techstar.api.util.HttpClientHeper;
import com.techstar.api.util.TrafficApiProperties;
import com.techstar.common.jackson.JacksonMapper;
import com.techstar.common.pojo.InterfaceType;
import com.techstar.common.utils.InterfaceUtils;
import com.techstar.roma.http.GatewayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.List;

/**
 * 
 * @author EinIce
 * @description 各枢纽旅客运输量变化趋势
 * @date 2021年9月9日
 */
//@Service
//public class HingeSituationPassengerVolumeService extends ParentOfListService<HingeSituationPassengerVolume> {
//
//	@Autowired
//	private HingeSituationPassengerVolumeRepos beanRepos;
//
//	@Autowired
//	private ConfigProperties configProperties; // 配置文件注入
//
//	public void query(String hub_type) {
//		try {
//		gateWayApiProperties = configProperties.romaHingeSituationApiProperties();
//		trafficApiProperties = configProperties.hspvApiProperties();
//		String currentTime = URLEncoder.encode(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"), "UTF-8").replaceAll("\\+", "%20");
//		trafficApiProperties.param = String.format("current_date=%s&end_date=%s&hub_type=%s", currentTime, currentTime, hub_type);
//		interfaceType = InterfaceType.HINGE_SITUATION_PASSENGER_VOLUME;
//		super.beanRepos = beanRepos;
//		sendHttp(new TypeReference<List<HingeSituationPassengerVolume>>() {}, new String[] {"data"});
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
//	}
//}


@Slf4j
@Service
public class HingeSituationPassengerVolumeService {

	private static final ObjectMapper objectMapper = JacksonMapper.instance.objectMapper;
	@Autowired
	private HingeSituationPassengerVolumeRepos beanRepos;

	@Autowired
	private ConfigProperties configProperties; // 配置文件注入

	public void query(String hub_type){
		try {
			GateWayApiProperties gateWayApiProperties = configProperties.romaHingeSituationApiProperties();
			TrafficApiProperties trafficApiProperties = configProperties.hspvApiProperties();
			if(gateWayApiProperties.testMode==true&&!"test".equals(trafficApiProperties.able)
					||"disable".equals(trafficApiProperties.able)) {
				log.info("DistrictCongestTrendService out");
				return;
			}
			String currentTime = URLEncoder.encode(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"), "UTF-8").replaceAll("\\+", "%20");
//            trafficApiProperties.param = String.format("end_date=%s&current_date=%s&hub_type=%s", currentTime, currentTime, hub_type);
			trafficApiProperties.param = String.format("current_date=%s&end_date=%s&hub_type=%s", currentTime, currentTime, hub_type);
			HttpGet httpGet = new HttpGet();
			httpGet = (HttpGet) GatewayService.GateWay(httpGet,gateWayApiProperties, trafficApiProperties);
			CloseableHttpResponse response = null;
			String result = null;
			response = HttpClientHeper.instance.httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity);
			log.info(result);
			JsonNode jsonNode = objectMapper.readTree(result.toString()).get("data");

			try {
				InterfaceType.HINGE_SITUATION_PASSENGER_VOLUME.setApiUrl(httpGet.toString());
				InterfaceType.HINGE_SITUATION_PASSENGER_VOLUME.setClassName(this.getClass().getSimpleName());
				InterfaceUtils.filtrateDate(InterfaceType.HINGE_SITUATION_PASSENGER_VOLUME, DateTime.now(), jsonNode);
			} catch (Exception e) {
				log.error("接口记录失败-"+this.getClass().getSimpleName()+":"+e.getMessage());
			}
			if(jsonNode==null)return;
			List<HingeSituationPassengerVolume> list = objectMapper.readValue(jsonNode.get("passenger_volume_info").toString(),new TypeReference<List<HingeSituationPassengerVolume>>() {});
			beanRepos.saveAll(list);
			List<HingeSituationPassengerVolume> list1 = objectMapper.readValue(jsonNode.get("passenger_volume_info_year").toString(),new TypeReference<List<HingeSituationPassengerVolume>>() {});
			beanRepos.saveAll(list1);
			List<HingeSituationPassengerVolume> list2 = objectMapper.readValue(jsonNode.get("passenger_volume_info_season").toString(),new TypeReference<List<HingeSituationPassengerVolume>>() {});
			beanRepos.saveAll(list2);
			response.close();

		}catch (Exception e){
			e.printStackTrace();
			log.error(e.getMessage());

		}
	}

}